﻿@using GlobalResources
@model Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Models.DeviceDetailModel


@if ((Model.HubEnabledState != null) || Model.CanRemoveDevice)
{
    <section class="details_grid_header">
        <div class="details_grid_header_image_container">
            @{ 
                var tagImage = Model.DevicePropertyValueModels.Where(m => m.Name == "tags." + Constants.DeviceIconTagName).FirstOrDefault();
                var defaultImage = Model.IsSimulatedDevice ? "/Content/img/IoT.svg" : "/Content/img/device_default.svg";
                var image = tagImage != null && !string.IsNullOrWhiteSpace(tagImage.Value) ?
                    ViewBag.IconBaseUrl + tagImage.Value : defaultImage;
            }
            <img src="@image" class="details_grid_header_image" />
            @if (Model.IsDeviceEditEnabled)
            {
            <a href="@Url.Action("EditIcon", "Device", new { deviceId = Model.DeviceID })"><img src="~/Content/img/edit_image.svg" class="details_grid_header_image_edit" title="@Strings.EditDeviceIcon" /></a>
            }
        </div>
        <div class="details_grid_header_actions">
        @if (Model.HubEnabledState.HasValue)
        {
            if (Model.CanDisableDevice)
            {
                <p class="grid_detail_value">
                    <a id="deviceExplorer_deactivateDevice"
                       data-hubenabledstate="@Model.HubEnabledState.ToString().ToLower()"
                       class="not_disable">
                        @Strings.ResourceManager.GetString(Model.HubEnabledState + "EnabledDevice")
                    </a>
                </p>
            }

            if (Model.CanAddRule)
            {
                <p class="grid_detail_value">
                    @Html.ActionLink(
                    Strings.AddRule, "EditRuleProperties", "DeviceRules",
                    new
                    {
                        deviceId = Model.DeviceID,
                        createRule = true
                    },
                    new
                    {
                        id = "addRuleLink"
                    })
                </p>
            }

            <p class="grid_detail_value">
                @Html.ActionLink(
                Strings.DeviceCommands, "Index", "DeviceCommand",
                new
                {
                    deviceId = Model.DeviceID
                },
                new
                {
                    id = "sendCommandLink"
                })
            </p>

            <p class="grid_detail_value">
                @Html.ActionLink(
               Strings.DeviceMethods, "Index", "DeviceMethod",
               new
               {
                   deviceId = Model.DeviceID
               },
               new
               {
                   id = "invokeMethodLink"
               })
            </p>
        }

        @if (Model.CanRemoveDevice)
        {
            <p id="removeDeviceLink"
               class="grid_detail_value">
                @Html.ActionLink(Strings.RemoveDevice,
                    "RemoveDevice",
                    "Device",
                    new { deviceId = Model.DeviceID },
                    null)
            </p>
        }
        </div>
    </section>
}

@if (Model.IsCellular)
{
    // save this to cache to save having to load device again if user decides to retrieve SIM
    <script>
        $(function() {
            IoTApp.Helpers.IccidState.saveIccidToCookie("@Model.Iccid");
        });
    </script>
    @Html.Partial("_DeviceDetailsCellular", Model);
}
else
{
    // clear any existing sim information as this device does not have any SIM information
    <script>
        $(function() {
            IoTApp.Helpers.IccidState.saveIccidToCookie(null);
        });
    </script>
}

@(Model.HubEnabledState != null ? 
    Html.Partial("_DeviceDetailsDeviceProperties", Model) : 
    Html.Partial("_DeviceDetailsNotRegistered", Model))

@if (Model.HubEnabledState == true)
{
    <script>
        $(function () {
            $('#removeDeviceLink').hide();
        });
    </script>
}